package com.unicom.activity;

import java.util.ArrayList;

import com.unicom.adapter.HomeMyStoryAdapter;
import com.unicom.fragment.AllStoryFragment;
import com.unicom.fragment.BookmarkFragment;
import com.unicom.fragment.HistoryFragment;
import com.unicom.mystory.R;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MyStoryHomeActivity extends FragmentActivity implements OnClickListener,OnPageChangeListener{
	private FragmentManager mMageger;
	private Button btAllStory, btBookMark, btHistory;
	private ImageView ivMenu, ivSearch;
	private TextView tvName;
	private EditText btSearch;
	private ViewPager vpHome;
	private ArrayList<Fragment> listFragment;
	private AllStoryFragment allStoryFragment;
	private BookmarkFragment bookmarkFragment;
	private HistoryFragment historyFragment;
	private HomeMyStoryAdapter homeMyStoryAdapter;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mystory_home);
		mMageger = getSupportFragmentManager();
		initView();
	}

	private void initView() {
		btAllStory = (Button) findViewById(R.id.all_story);
		btAllStory.setOnClickListener(this);
		btBookMark = (Button) findViewById(R.id.book_mark);
		btBookMark.setOnClickListener(this);
		btHistory = (Button) findViewById(R.id.history);
		btHistory.setOnClickListener(this);
		ivMenu = (ImageView) findViewById(R.id.title_menu);
		ivMenu.setOnClickListener(this);
		ivSearch = (ImageView) findViewById(R.id.title_search);
		ivSearch.setOnClickListener(this);
		tvName = (TextView) findViewById(R.id.title_name);
		vpHome = (ViewPager) findViewById(R.id.home_viewpager);
		btSearch = (EditText) findViewById(R.id.title_Search);
		btSearch.setVisibility(View.GONE);
		listFragment = new ArrayList<Fragment>();
		allStoryFragment = new AllStoryFragment();
		bookmarkFragment = new BookmarkFragment();
		historyFragment = new HistoryFragment();
		listFragment.add(allStoryFragment);
		listFragment.add(bookmarkFragment);
		listFragment.add(historyFragment);
		
		homeMyStoryAdapter = new HomeMyStoryAdapter(mMageger, listFragment);
		vpHome.setAdapter(homeMyStoryAdapter);
		
		vpHome.setOnPageChangeListener(this);
		showViewPagerTab(0);
	}
	private void showViewPagerTab(int position) {
		switch (position) {
		case HomeMyStoryAdapter.TAB_ALL_STORY:
			btAllStory.setSelected(true);
			btBookMark.setSelected(false);
			btHistory.setSelected(false);
			break;
		case HomeMyStoryAdapter.TAB_BOOKMARK:
			btAllStory.setSelected(false);
			btBookMark.setSelected(true);
			btHistory.setSelected(false);
			break;
		case HomeMyStoryAdapter.TAB_HISTORY:
			btAllStory.setSelected(false);
			btBookMark.setSelected(false);
			btHistory.setSelected(true);
			break;

		default:
			break;
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.all_story:
			vpHome.setCurrentItem(HomeMyStoryAdapter.TAB_ALL_STORY);
			showViewPagerTab(HomeMyStoryAdapter.TAB_ALL_STORY);
			break;
		case R.id.book_mark:
			vpHome.setCurrentItem(HomeMyStoryAdapter.TAB_BOOKMARK);
			showViewPagerTab(HomeMyStoryAdapter.TAB_BOOKMARK);
			break;
		case R.id.history:
			vpHome.setCurrentItem(HomeMyStoryAdapter.TAB_HISTORY);
			showViewPagerTab(HomeMyStoryAdapter.TAB_HISTORY);
			break;
		case R.id.title_menu:
			
			break;
		case R.id.title_search:
			ivMenu.setVisibility(View.GONE);
			tvName.setVisibility(View.GONE);
			ivSearch.setVisibility(View.GONE);
			btSearch.setVisibility(View.VISIBLE);
			break;

		default:
			break;
		}
	}
	int test;
	@Override
	public void onPageScrollStateChanged(int state) {
		test = 0;
		if(state == 1){
			test = 1;
		}
	}

	@Override
	public void onPageScrolled(int state, float arg1, int arg2) { 
		if(state == 2 && arg1==0.0 && arg2 == 0 && test == 1){
			vpHome.setCurrentItem(0);
		} 
	}

	@Override
	public void onPageSelected(int state) {
		showViewPagerTab(state);
	}
}
